#include "zero_return_mode.h"
#include "ui_zero_return_mode.h"

ZeroReturnMode::ZeroReturnMode(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ZeroReturnMode)
{
    ui->setupUi(this);

    initializeBackground(ui->origin_widget_Logo);
    setTable(ui->tableWidget);
    updateMantuableData();

    connectSignalsAndSlots();
}

void ZeroReturnMode::initializeBackground(QWidget *widget)
{
    widget->setAutoFillBackground(true);
    QPalette palette = widget->palette();
    QPixmap background(":/image/AEF_LOGO.jpeg");
    QPixmap scaledBackground = background.scaled(widget->size(), Qt::IgnoreAspectRatio);
    palette.setBrush(QPalette::Background, scaledBackground);
    widget->setPalette(palette);
}

void ZeroReturnMode::setTable(QTableWidget* table)
{
    table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    table->verticalHeader()->setHidden(true);

    table->setColumnCount(5);
    table->setRowCount(5);
    table->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    table->setColumnWidth(0,55);
    table->setColumnWidth(1,135);
    table->setColumnWidth(2,45);
    table->setColumnWidth(3,45);
    table->setColumnWidth(4,45);

    table->setRowHeight(0,70);
    table->setRowHeight(1,70);
    table->setRowHeight(2,70);
    table->setRowHeight(3,70);
    table->setRowHeight(4,70);

    table->setHorizontalHeaderLabels(QStringList() << "  " << "当前位置/这是啥" << "原点" << "极限" << "刹车");
    QHeaderView* header = table->horizontalHeader();
    QBrush brush(Qt::black);
    for (int i = 0; i < header->count(); i++) {
        QTableWidgetItem *item = new QTableWidgetItem(header->model()->headerData(i, Qt::Horizontal).toString());
        item->setTextColor(brush.color());
        item->setTextAlignment(Qt::AlignVCenter | Qt::AlignLeft);
        table->setHorizontalHeaderItem(i, item);

        QFont font = item->font();
        font.setPointSize(13);
        item->setFont(font);
    }

    QStringList axisName;
    axisName << " Y轴" << " MX轴" << " SX轴" << " MZ轴" << " SX轴";
    for(int row = 0; row < 5; row++)
    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(axisName[row]);
        item->setForeground(Qt::black);
        item->setBackgroundColor(Qt::white);
        item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        table->setItem(row, 0, item);

        QFont font = item->font();
        font.setPointSize(13);
        item->setFont(font);
    }

    for(int row = 0; row < 5; row++)
    {
        setTableCellText(table, row, 1, "136.6 / 57");
    }
    for(int row = 0; row < 5; row++){
        setTableCellColor(table, row, 2, "灰色");
    }
    for(int row = 0; row < 5; row++){
        setTableCellColor(table, row, 3, "红色");
    }
    for(int row = 0; row < 5; row++){
        setTableCellColor(table, row, 4, "绿色");
    }
}

void ZeroReturnMode::addSpeedClicked()
{
    emit addSpeedValue();
    ui->speed_flexibleLabel->setText(pendantInterfaceData.speedMutableLabel);
}

void ZeroReturnMode::decSpeedClicked()
{
    emit decSpeedValue();
    ui->speed_flexibleLabel->setText(pendantInterfaceData.speedMutableLabel);
}

void ZeroReturnMode::updateMantuableData()
{
    setSpeedValue(ui->speed_flexibleLabel);
}

void ZeroReturnMode::pushReturnMenu()
{
    emit returnMenu();
}

void ZeroReturnMode::pushReturnTeachMode()
{
    emit returnTeachMode();
}

void ZeroReturnMode::pushReturnCalTimer()
{
    emit returnCalTimer();
}

void ZeroReturnMode::pushReturnManual()
{
    emit returnManual();
}

void ZeroReturnMode::pushReturnAutomatic()
{
    emit returnAutomatic();
}

ZeroReturnMode::~ZeroReturnMode()
{
    delete ui;
}

void ZeroReturnMode::connectSignalsAndSlots()
{
    connect(ui->returnMenu, &QPushButton::clicked, this, &ZeroReturnMode::pushReturnMenu);
    connect(ui->manualReturnTeachMode_2, &QPushButton::clicked, this, &ZeroReturnMode::pushReturnTeachMode);
    connect(ui->manualReturnCaltimer_2, &QPushButton::clicked, this, &ZeroReturnMode::pushReturnCalTimer);
    connect(ui->pb_manual, &QPushButton::clicked, this, &ZeroReturnMode::pushReturnManual);
    connect(ui->pb_automatic, &QPushButton::clicked, this, &ZeroReturnMode::pushReturnAutomatic);

    connect(ui->pb_addSpeed, &QPushButton::clicked, this, &ZeroReturnMode::addSpeedClicked);
    connect(ui->pb_decSpeed, &QPushButton::clicked, this, &ZeroReturnMode::decSpeedClicked);
}

void ZeroReturnMode::displayTechnParam(Techn_Param_ptr tech_param)
{
    setTableCellText(ui->tableWidget, 1, 1, getStringFromTechnParam(tech_param->AxisMX.AxisPos, tech_param->AxisMX.EncodePos));

    if(tech_param->AxisMX.IsZero)
    {
        setTableCellColor(ui->tableWidget, 1, 2, "绿色");
    }
    else
    {
        setTableCellColor(ui->tableWidget, 1, 2, "红色");
    }
}
